package org.wu.framework.lazy.orm.core.source.questdb;

import lombok.Data;
import org.wu.framework.core.NormalUsedString;
import org.wu.framework.core.utils.ObjectUtils;
import org.wu.framework.lazy.orm.core.persistence.conf.LazyTableFieldEndpoint;
import org.wu.framework.lazy.orm.core.persistence.conf.field.AbstractLazyTableFieldEndpoint;
import org.wu.framework.lazy.orm.core.stereotype.LazyTableField;

/**
 * mysql Java字段转换成 questdb column对象处理
 */
@Data
public class QuestDBColumnEndpoint extends AbstractLazyTableFieldEndpoint {
    /**
     * exception at run time.
     *
     * @return a clone of this instance.
     * @throws CloneNotSupportedException if the object's class does not
     *                                    support the {@code Cloneable} interface. Subclasses
     *                                    that override the {@code clone} method can also
     *                                    throw this exception to indicate that an instance cannot
     *                                    be cloned.
     * @see Cloneable
     */
    @Override
    public LazyTableFieldEndpoint clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String getColumnType() {
        return ObjectUtils.isEmpty(super.getColumnType()) ? LazyTableField.FieldType.getQuestDBTypeByClass(super.getClazz()) : super.getColumnType();
    }

    /**
     * @return 行sql
     * description 创建SQL column
     * "`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',\n" +
     * @author Jiawei Wu
     * @date 2020/12/31 9:19 下午
     **/
    @Override
    public String columnSQL() {

        return getColumnName() + NormalUsedString.SPACE +
                getColumnType() + NormalUsedString.SPACE +
                (
                        isNotNull() ?
                                NormalUsedString.SPACE + NormalUsedString.NOT + NormalUsedString.SPACE + NormalUsedString.NULL + NormalUsedString.SPACE :
                                NormalUsedString.SPACE
                )
                +
                (
                        ObjectUtils.isEmpty(getDefaultValue()) ?
                                NormalUsedString.SPACE :
                                NormalUsedString.SPACE + NormalUsedString.DEFAULT + NormalUsedString.SPACE + getDefaultValue() + NormalUsedString.SPACE) +
                (isKey() ? "PRIMARY KEY" + NormalUsedString.SPACE : NormalUsedString.EMPTY) +
                NormalUsedString.SPACE + getExtra() + NormalUsedString.SPACE +
                NormalUsedString.COMMENT + NormalUsedString.SPACE +
                NormalUsedString.SINGLE_QUOTE + getComment() + NormalUsedString.SINGLE_QUOTE;
    }

    /**
     * ALTER COLUMN `is_deleted` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否删除`;
     */
    @Override
    public String modifyColumnSQL() {
        return NormalUsedString.ALTER + NormalUsedString.SPACE + NormalUsedString.COLUMN + NormalUsedString.SPACE + columnSQL() + NormalUsedString.SPACE;
    }

    @Override
    public String toString() {
        return super.toString();
    }
}
